package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/orderProcess/model"
	"strconv"
)

type OrderProcessController struct {
	Ctx iris.Context
	Order *model.Order
	ExpressChange *model.ExpressChange
}

func (opc *OrderProcessController)PostPlaceorder() mvc.Result {

	opc.Order = new(model.Order)

	var putOrder model.PutOrder
	opc.Ctx.ReadJSON(&putOrder)

	res, err := opc.Order.PlaceOrder(&putOrder)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (opc *OrderProcessController)PostQueryaudit() mvc.Result {

	opc.Order = new(model.Order)

	adviserIdStr := opc.Ctx.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := opc.Order.QueryAudit(adviserId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (opc *OrderProcessController)PostConsultantconfirmationorder() mvc.Result {

	opc.Order = new(model.Order)

	orderIdStr := opc.Ctx.FormValue("orderId")
	orderId, err := strconv.ParseInt(orderIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := opc.Order.ConsultantConfirmationOrder(orderId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (opc *OrderProcessController)PostToexamine() mvc.Result {

	opc.Order = new(model.Order)

	adviserIdStr := opc.Ctx.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	auditIdStr := opc.Ctx.FormValue("auditId")
	auditId, err := strconv.ParseInt(auditIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := opc.Order.ToExamine(adviserId, auditId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (opc *OrderProcessController)PostReviewreturn() mvc.Result {

	opc.Order = new(model.Order)

	adviserIdStr := opc.Ctx.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	auditIdStr := opc.Ctx.FormValue("auditId")
	auditId, err := strconv.ParseInt(auditIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := opc.Order.ReviewReturn(adviserId, auditId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (opc *OrderProcessController)PostOrderaddresschange() mvc.Result {

	opc.ExpressChange = new(model.ExpressChange)

	orderIdStr := opc.Ctx.FormValue("orderId")
	orderId, err := strconv.ParseInt(orderIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	adviserIdStr := opc.Ctx.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	expressStr := opc.Ctx.FormValue("express")
	express, err := strconv.Atoi(expressStr)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := opc.ExpressChange.OrderAddressChange(orderId, adviserId, express)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (opc *OrderProcessController)PostQueryexpressaudit() mvc.Result {

	opc.ExpressChange = new(model.ExpressChange)

	adviserIdStr := opc.Ctx.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := opc.ExpressChange.QueryExpressAudit(adviserId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (opc *OrderProcessController)PostAddressconfirmation() mvc.Result {

	opc.ExpressChange = new(model.ExpressChange)

	adviserIdStr := opc.Ctx.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	expressAuditIdStr := opc.Ctx.FormValue("expressAuditId")
	expressAuditId, err := strconv.ParseInt(expressAuditIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := opc.ExpressChange.AddressConfirmation(adviserId, expressAuditId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}























































































